package handler

import (
	"encoding/json"
	"gopkg.in/mgo.v2/bson"
	"svvenus/conf"
	"svvenus/util/uttype"
	"svvenus/vgame/logic"
	"svvenus/vpub/worker"
	"svvenus/znet"
	"svvenus/znet/zpb"
	"time"
)

func GmGetPlayer(uid string) *logic.Player {
	if bson.IsObjectIdHex(uid) {
		return logic.PlayerGet(uid, logic.PlayerDataGetModeAll)
	}
	pid, ok := logic.PlayerGetPidFormName(uid)
	if ok {
		return logic.PlayerGet(pid, logic.PlayerDataGetModeAll)
	}
	return nil
}

// CGGmAction
// @Description: GM操作
// @param request
func CGGmAction(request *znet.Request) {
	rsp := &zpb.G2CGmAction{}
	rid := zpb.EPid_GCGmAction
	var req zpb.C2GGmAction
	if !request.GetPb(&req) {
		return
	}

	if req.Token != "7u8i9o0p" {
		request.Send(rid, conf.EProtocolResult管理员登陆密码错误, rsp)
		return
	}

	if req.Action == "改变货币" {
		player := GmGetPlayer(req.Uid)
		if player == nil {
			request.Send(rid, conf.EProtocolResult玩家不存在, rsp)
			return
		}
		if len(req.Args) < 2 {
			request.Send(rid, conf.EProtocolResult错误的修改参数, rsp)
			return
		}
		v0 := uttype.StringToInt(req.Args[0])
		v1 := uttype.StringToInt64(req.Args[1])
		player.MoneyChange(v0, v1, zpb.RecSource_rsGm)
		player.PushInfoBase()
	} else if req.Action == "道具添加" {
		player := GmGetPlayer(req.Uid)
		if player == nil {
			request.Send(rid, conf.EProtocolResult玩家不存在, rsp)
			return
		}
		if len(req.Args) < 2 {
			request.Send(rid, conf.EProtocolResult错误的修改参数, rsp)
			return
		}
		v0 := uttype.StringToInt(req.Args[0])
		v1 := uttype.StringToInt64(req.Args[1])
		player.ItemAdd(v0, v1, zpb.RecSource_rsGm)
		player.PushItemInfoList()
	} else if req.Action == "装备添加" {
		player := GmGetPlayer(req.Uid)
		if player == nil {
			request.Send(rid, conf.EProtocolResult玩家不存在, rsp)
			return
		}
		if len(req.Args) < 1 {
			request.Send(rid, conf.EProtocolResult错误的修改参数, rsp)
			return
		}
		v0 := uttype.StringToInt(req.Args[0])
		player.EquipAdd(v0, zpb.RecSource_rsGm)
		player.PushEquipInfoList()
	} else if req.Action == "服务器存档" ||
		req.Action == "服务器关闭" ||
		req.Action == "服务器锁定" ||
		req.Action == "服务器解锁" {
		if len(req.Args) < 1 {
			request.Send(rid, conf.EProtocolResult错误的修改参数, rsp)
			return
		}
		if req.Args[0] != "zzz" {
			request.Send(rid, conf.EProtocolResult管理员登陆密码错误, rsp)
			return
		}
		worker.CmdlineWorkerAddCmd(req.Action)
	} else if req.Action == "发送玩家邮件" {
		if len(req.Args) < 1 {
			request.Send(rid, conf.EProtocolResult错误的修改参数, rsp)
			return
		}
		gmail := new(zpb.GmMailInfo)
		err := json.Unmarshal([]byte(req.Args[0]), &gmail)
		if err != nil {
			request.Send(rid, conf.EProtocolResult错误的修改参数, rsp)
			return
		}
		listIds := uttype.StringToListString(req.Uid)
		for _, uid := range listIds {
			player := GmGetPlayer(uid)
			if player == nil {
				//request.Send(rid, conf.EProtocolResult玩家不存在, rsp)
				//return
				continue
			}
			mail := logic.NewMail()
			mail.Title = gmail.Title
			mail.Body = gmail.Body
			mail.SenderName = "管理员"
			mail.CreateTime = time.Now()
			for _, v := range gmail.Awards {
				mail.AddAward(logic.PbAward2PcAward(v))
			}
			mail.MailType = int32(zpb.MailType_MailTypeGm)
			mail.RecSource = int32(zpb.RecSource_rsGm)
			player.MailAddToPlayer(mail)
		}
	} else if req.Action == "发送全服邮件" {
		if len(req.Args) < 1 {
			request.Send(rid, conf.EProtocolResult错误的修改参数, rsp)
			return
		}
		gmail := new(zpb.GmGMailInfo)
		err := json.Unmarshal([]byte(req.Args[0]), &gmail)
		if err != nil {
			request.Send(rid, conf.EProtocolResult错误的修改参数, rsp)
			return
		}
		logic.GlobalMailAdd(gmail)
	} else {
		request.Send(rid, conf.EProtocolResult错误的修改类型, rsp)
		return
	}

	request.Send(rid, 0, rsp)
}
